package com.choosefine.it.basedata.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.model.User;
import com.choosefine.it.basedata.model.dto.FindUserDTO;
import com.choosefine.it.basedata.model.dto.UserPhoneBindDTO;
import com.choosefine.it.basedata.model.dto.UserSearchDTO;
import com.choosefine.it.basedata.model.vo.UserSearchVO;
import com.choosefine.it.basedata.model.vo.UserVO;
import com.choosefine.it.basedata.service.UserService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.Assert;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.PagedResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 用户相关控制层
 * 
 * @author guwenwei 2016/11/16
 * @author 戚羿辰 2017/1/10
 */
@RestController
@RequestMapping("/basedata/user")
@Api(value = "用户相关", description = "顾文伟, 戚羿辰")
public class UserController extends BaseController
{

	Logger		logger	= LoggerFactory.getLogger(UserController.class);

	@Autowired
	UserService	userService;

	/**
	 * 添加用户
	 * 
	 * @param user
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "存储用户 last 2017/2/15", notes = "传入用户对象，存储相关用户用户基础信息")
	@ApiResponses({ @ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.POST)
	public String save(@RequestBody User user) throws Exception {
		String result = null;
		userService.insert(user);
		result = responseSuccess(user.getId(), ResultCode.SUCCESS_CREATED, ResultCode.MSG_CREATE_SUCCESS);
		return result;
	}

	/**
	 * 修改用户
	 * @param user
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "修改用户 last 2017/2/15", notes = "传入用户对象，根据ID更改相关用户用户基础信息")
	@ResponseBody
	@ApiResponses({ @ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.PUT)
	public String update(@RequestBody User user) {
		String result = null;
		Integer num = userService.update(user);
		result = responseSuccess(num, ResultCode.SUCCESS_CREATED, ResultCode.MSG_UPDATE_SUCCESS);
		return result;
	}

	/**
	 * 根据编号获取信息
	 * @param userCode
	 * @return
	 * @throws Exception
	 * @throws
	 */
	@ApiOperation(value = "根据用户编号取用户信息 last 2017/2/15", notes = "传入用户编号，根据用户编号获取用户信息")
	@ApiImplicitParam(name = "userCode", value = "用户编号", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "/UserCode/{userCode}", method = RequestMethod.GET)
	public String getUserByUserCode(@PathVariable String userCode) throws Exception {
		UserSearchVO user = userService.findByUserCode(userCode);
		return responseSuccess(user);
	}

	/**
	 * 根据id获取信息
	 * @param id
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "根据用户ID取用户信息 last 2017/2/15", notes = "传入主键ID，根据主键ID获取用户信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String getUserById(@PathVariable Integer id) {
		User user = userService.findByPrimaryKey(id);
		return responseSuccess(user);
	}
	
	/**
	 * 根据id批量禁用用户
	 * @param id
	 * @param isValid
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "根据用户id批量禁用启用用户 last 2017/2/28", notes = "")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "ids", value = "用户id", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "isValid", value = "启用状态", required = true, dataType = "Integer", paramType = "path") })
	@RequestMapping(value = "/batchValid/{ids}/{isValid}", method = RequestMethod.DELETE)
	public String valid(@PathVariable(required = true) String ids, @PathVariable Integer isValid) {
		Map<String, Object> params = new HashMap<>();
		params.put("isValid", isValid);
		params.put("ids", ids.split(","));
		params.put("utime", DateUtils.getNowTime());
		Integer num = userService.deleteByIds(params);
		return responseSuccess(num);
	}

	/**
	 * 根据id删除用户
	 * @param id
	 * @param isValid
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "根据用户id禁用启用用户 last 2017/2/15", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "isValid", value = "启用状态", required = true, dataType = "Integer", paramType = "path") })
	@RequestMapping(value = "/valid/{id}/{isValid}", method = RequestMethod.DELETE)
	public String valid(@PathVariable(required = true) Integer id, @PathVariable Integer isValid) {
		Map<String, Object> params = new HashMap<>();
		params.put("isValid", isValid);
		params.put("id", id);
		params.put("utime", DateUtils.getNowTime());
		Integer num = userService.deleteById(params);
		return responseSuccess(num);
	}
	
	/**
	 * 根据id删除用户
	 * @param id
	 * @param isValid
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "根据用户id删除用户 last 2017/2/15", notes = "")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Integer", paramType = "path")})
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable(required = true) Integer id) {
		Integer num = userService.deleteByPrimaryKey(id);
		return responseSuccess(num);
	}

	/**
	 * 获取用户列表(简单搜索)
	 * @param pageNum
	 * @param pageSize
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "获取用户列表 last 2017/1/6", notes = "根据url分页的参数来查询用户的信息列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "path") })
	@RequestMapping(method = RequestMethod.GET, value = "/UserList/{pageNum}/{pageSize}", produces = "application/json")
	public String getUserList(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
		List<User> userlist = userService.findAllByPage(pageNum, pageSize);
		return responseArraySuccess(userlist);
	}

	/**
	 * 筛选用户
	 * @param object
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "显示所有类型登录账户 last 2017/1/6")
	@PostMapping(value = "/UserList")
	public String selectUserList(@RequestBody UserSearchDTO object) {
		Assert.assertNotNull(object);
		PageDTO pageDto = new PageDTO();
		pageDto.setPageNum(object.getPageNum());
		pageDto.setPageSize(object.getPageSize());
		List<UserSearchVO> userList = userService.selectUserBy(object, pageDto);
		return responseArraySuccess(PagedResult.toPageInfo(userList));
	}
	
	/**
	 * 筛选用户
	 * @param object
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "筛选租户 (包括公司和个人)last 2017/3/2")
	@GetMapping(value = "/User")
	public String selectUserList(@ModelAttribute FindUserDTO object) {
		Assert.assertNotNull(object);
		PageDTO pageDto = new PageDTO();
		pageDto.setPageNum(object.getPageNum());
		pageDto.setPageSize(object.getPageSize());
		List<UserVO> userList = userService.selectUser(object, pageDto);
		return responseArraySuccess(PagedResult.toPageInfo(userList));
	}
	
	/**
	 * 修改用户手机号绑定
	 * @param user
	 * @return
	 * @throws
	 */
	@ApiOperation(value = "修改用户 last 2017/3/7", notes = "传入用户对象，根据ID更改相关用户用户基础信息")
	@ResponseBody
	@ApiResponses({ @ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@PutMapping("/changePhone")
	public String changePhone(@RequestBody UserPhoneBindDTO userPhoneBindDTO) {
		String result = null;
		Integer num = userService.updatePhone(userPhoneBindDTO.getOldPhone(), userPhoneBindDTO.getNewPhone());
		result = responseSuccess(num, ResultCode.SUCCESS_CREATED, ResultCode.MSG_UPDATE_SUCCESS);
		return result;
	}
}
